package com.xky.sportstestdemo01.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xky.sportstestdemo01.common.Result;
import com.xky.sportstestdemo01.pojo.entity.PhysicalFitnessScores;
import com.xky.sportstestdemo01.service.PhysicalFitnessScoresService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 体测成绩分数表(PhysicalFitnessScores)表控制层
 *
 * @author twt
 * @since 2024-11-08 13:49:57
 */
@CrossOrigin
@RestController
@RequestMapping("physicalFitnessScores")
public class PhysicalFitnessScoresController {

    @Autowired
    PhysicalFitnessScoresService physicalFitnessScoresService;


    /**
     * 查询全部
     */
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<PhysicalFitnessScores> physicalFitnessScoresList = physicalFitnessScoresService.list(new QueryWrapper<PhysicalFitnessScores>().orderByDesc("physicalFitnessScoresId"));
        return Result.success(physicalFitnessScoresList);
    }

    /**
     * 新增
     */
    @PostMapping("/add")
    public Result add(@RequestBody PhysicalFitnessScores physicalFitnessScores) {
        try {
            physicalFitnessScoresService.save(physicalFitnessScores);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据库错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody PhysicalFitnessScores physicalFitnessScores) {
        physicalFitnessScoresService.updateById(physicalFitnessScores);
        return Result.success();
    }

    /**
     * 删除
     */

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        physicalFitnessScoresService.removeById(id);
        return Result.success();
    }

    /**
     * 根据学生姓名查询信息
     *
     * @auther 唐文涛
     */
    @GetMapping("/selectByName/{name}")
    public Result selectByName(@PathVariable String name) {
        physicalFitnessScoresService.selectByName(name);
        return Result.success();
    }

    /**
     * @author 堂文水寿
     * 计算体测总成绩
     */
    @GetMapping("/calculateTotalScore")
    public Result calculateTotalScore() {
        //TODO
        return Result.success();
    }

    /**
     * @author 堂文水寿
     * 计算体测总成绩
     */
    //计算体测的每一项成绩
    @GetMapping("/calculateEachScore")
    public Result calculateEachScore() {
        //TODO
        return Result.success();
    }


}


